package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Vehicle;
@Entity
@Table(name="booking")
public class Booking extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	
	public String customer; //用车客户
	
	public String customer_name; //订车人
	
	public String customer_phone; //联系电话
	
	public Integer passenger_number; //乘员数
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date start_date; //用车日期
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date end_date; //结束日期
	
	public Integer useday_number; //用车天数
	
	public Integer plan_route; //计划行程
	@ManyToOne
	public Vehicle vehicle; //车辆厂牌
	
	public Integer vehicle_count; //车辆数
	
	public Integer set_count; //座位数
	
	public String stop_place; //停车地点
	@JsonSerialize(using=JsonDatetimeFormatter.class)
	public Date stopstart_time; //停车时间
	@JsonSerialize(using=JsonDatetimeFormatter.class)
	public Date stopend_time; //结束时间
	
	public Integer limit_mileage; //额定里程
	
	public Integer limit_price; //额定车价
	
	public Integer parking_fees; //停车费(估）
	
	public Integer passroad_fees; //过路费(估）
	
	public String payer; //承担方
	
	public Integer allowance; //补贴
	
	public String checkout_type; //结算方式
	
	public String work_type; //业务性质
	
	public String name; //承接人
	
	public Integer check_number; //核定数
	
	public String comment; //备注
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date write_date; //登记日期
	public static Model.Finder<Integer, Booking> find = new Model.Finder<Integer, Booking>(Integer.class, Booking.class);

	public static Page<Booking> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Booking> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setCustomer(String d) {
		customer= d;
	}
	public String getCustomer() {
		return customer;
	}
	public void setCustomer_name(String d) {
		customer_name= d;
	}
	public String getCustomer_name() {
		return customer_name;
	}
	public void setCustomer_phone(String d) {
		customer_phone= d;
	}
	public String getCustomer_phone() {
		return customer_phone;
	}
	public void setPassenger_number(Integer d) {
		passenger_number= d;
	}
	public Integer getPassenger_number() {
		return passenger_number;
	}
	public void setStart_date(Date d) {
		start_date= d;
	}
	public Date getStart_date() {
		return start_date;
	}
	public void setEnd_date(Date d) {
		end_date= d;
	}
	public Date getEnd_date() {
		return end_date;
	}
	public void setUseday_number(Integer d) {
		return;
	}
	public Integer getUseday_number() {
		return  (int)((end_date.getTime()-start_date.getTime())/(24*60*60*1000));
	}
	public void setPlan_route(Integer d) {
		plan_route= d;
	}
	public Integer getPlan_route() {
		return plan_route;
	}
	public void setVehicle(Vehicle d) {
		vehicle= d;
	}
	public Vehicle getVehicle() {
		return vehicle;
	}
	public void setVehicle_count(Integer d) {
		vehicle_count= d;
	}
	public Integer getVehicle_count() {
		return vehicle_count;
	}
	public void setSet_count(Integer d) {
		set_count= d;
	}
	public Integer getSet_count() {
		return set_count;
	}
	public void setStop_place(String d) {
		stop_place= d;
	}
	public String getStop_place() {
		return stop_place;
	}
	public void setStopstart_time(Date d) {
		stopstart_time= d;
	}
	public Date getStopstart_time() {
		return stopstart_time;
	}
	public void setStopend_time(Date d) {
		stopend_time= d;
	}
	public Date getStopend_time() {
		return stopend_time;
	}
	public void setLimit_mileage(Integer d) {
		limit_mileage= d;
	}
	public Integer getLimit_mileage() {
		return limit_mileage;
	}
	public void setLimit_price(Integer d) {
		limit_price= d;
	}
	public Integer getLimit_price() {
		return limit_price;
	}
	public void setParking_fees(Integer d) {
		parking_fees= d;
	}
	public Integer getParking_fees() {
		return parking_fees;
	}
	public void setPassroad_fees(Integer d) {
		passroad_fees= d;
	}
	public Integer getPassroad_fees() {
		return passroad_fees;
	}
	public void setPayer(String d) {
		payer= d;
	}
	public String getPayer() {
		return payer;
	}
	public void setAllowance(Integer d) {
		allowance= d;
	}
	public Integer getAllowance() {
		return allowance;
	}
	public void setCheckout_type(String d) {
		checkout_type= d;
	}
	public String getCheckout_type() {
		return checkout_type;
	}
	public void setWork_type(String d) {
		work_type= d;
	}
	public String getWork_type() {
		return work_type;
	}
	public void setName(String d) {
		name= d;
	}
	public String getName() {
		return name;
	}
	public void setCheck_number(Integer d) {
		check_number= d;
	}
	public Integer getCheck_number() {
		return check_number;
	}
	public void setComment(String d) {
		comment= d;
	}
	public String getComment() {
		return comment;
	}
	public void setWrite_date(Date d) {
		write_date= d;
	}
	public Date getWrite_date() {
		return write_date;
	}
	public static Booking findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
	public static Booking findByCustomer(String d) {
		return find.where().eq("customer", d).findUnique();
	}
	public static String getCustomerJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getCustomer().toString(), v.getCustomer().toString()); 
		}
		return node.toString();
	}
	public static Booking findByCustomer_name(String d) {
		return find.where().eq("customer_name", d).findUnique();
	}
	public static String getCustomer_nameJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getCustomer_name().toString(), v.getCustomer_name().toString()); 
		}
		return node.toString();
	}
	public static Booking findByCustomer_phone(String d) {
		return find.where().eq("customer_phone", d).findUnique();
	}
	public static String getCustomer_phoneJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getCustomer_phone().toString(), v.getCustomer_phone().toString()); 
		}
		return node.toString();
	}
	public static Booking findByPassenger_number(Integer d) {
		return find.where().eq("passenger_number", d).findUnique();
	}
	public static String getPassenger_numberJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getPassenger_number().toString(), v.getPassenger_number().toString()); 
		}
		return node.toString();
	}
	public static Booking findByStart_date(Date d) {
		return find.where().eq("start_date", d).findUnique();
	}
	public static String getStart_dateJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getStart_date().toString(), v.getStart_date().toString()); 
		}
		return node.toString();
	}
	public static Booking findByEnd_date(Date d) {
		return find.where().eq("end_date", d).findUnique();
	}
	public static String getEnd_dateJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getEnd_date().toString(), v.getEnd_date().toString()); 
		}
		return node.toString();
	}
	public static Booking findByUseday_number(Integer d) {
		return find.where().eq("useday_number", d).findUnique();
	}
	public static String getUseday_numberJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getUseday_number().toString(), v.getUseday_number().toString()); 
		}
		return node.toString();
	}
	public static Booking findByPlan_route(Integer d) {
		return find.where().eq("plan_route", d).findUnique();
	}
	public static String getPlan_routeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getPlan_route().toString(), v.getPlan_route().toString()); 
		}
		return node.toString();
	}
	public static Booking findByVehicle(Vehicle d) {
		return find.where().eq("vehicle", d).findUnique();
	}
	public static String getVehicleJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getVehicle().toString(), v.getVehicle().toString()); 
		}
		return node.toString();
	}
	public static Booking findByVehicle_count(Integer d) {
		return find.where().eq("vehicle_count", d).findUnique();
	}
	public static String getVehicle_countJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getVehicle_count().toString(), v.getVehicle_count().toString()); 
		}
		return node.toString();
	}
	public static Booking findBySet_count(Integer d) {
		return find.where().eq("set_count", d).findUnique();
	}
	public static String getSet_countJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getSet_count().toString(), v.getSet_count().toString()); 
		}
		return node.toString();
	}
	public static Booking findByStop_place(String d) {
		return find.where().eq("stop_place", d).findUnique();
	}
	public static String getStop_placeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getStop_place().toString(), v.getStop_place().toString()); 
		}
		return node.toString();
	}
	public static Booking findByStopstart_time(Date d) {
		return find.where().eq("stopstart_time", d).findUnique();
	}
	public static String getStopstart_timeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getStopstart_time().toString(), v.getStopstart_time().toString()); 
		}
		return node.toString();
	}
	public static Booking findByStopend_time(Date d) {
		return find.where().eq("stopend_time", d).findUnique();
	}
	public static String getStopend_timeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getStopend_time().toString(), v.getStopend_time().toString()); 
		}
		return node.toString();
	}
	public static Booking findByLimit_mileage(Integer d) {
		return find.where().eq("limit_mileage", d).findUnique();
	}
	public static String getLimit_mileageJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getLimit_mileage().toString(), v.getLimit_mileage().toString()); 
		}
		return node.toString();
	}
	public static Booking findByLimit_price(Integer d) {
		return find.where().eq("limit_price", d).findUnique();
	}
	public static String getLimit_priceJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getLimit_price().toString(), v.getLimit_price().toString()); 
		}
		return node.toString();
	}
	public static Booking findByParking_fees(Integer d) {
		return find.where().eq("parking_fees", d).findUnique();
	}
	public static String getParking_feesJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getParking_fees().toString(), v.getParking_fees().toString()); 
		}
		return node.toString();
	}
	public static Booking findByPassroad_fees(Integer d) {
		return find.where().eq("passroad_fees", d).findUnique();
	}
	public static String getPassroad_feesJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getPassroad_fees().toString(), v.getPassroad_fees().toString()); 
		}
		return node.toString();
	}
	public static Booking findByPayer(String d) {
		return find.where().eq("payer", d).findUnique();
	}
	public static String getPayerJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getPayer().toString(), v.getPayer().toString()); 
		}
		return node.toString();
	}
	public static Booking findByAllowance(Integer d) {
		return find.where().eq("allowance", d).findUnique();
	}
	public static String getAllowanceJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getAllowance().toString(), v.getAllowance().toString()); 
		}
		return node.toString();
	}
	public static Booking findByCheckout_type(String d) {
		return find.where().eq("checkout_type", d).findUnique();
	}
	public static String getCheckout_typeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getCheckout_type().toString(), v.getCheckout_type().toString()); 
		}
		return node.toString();
	}
	public static Booking findByWork_type(String d) {
		return find.where().eq("work_type", d).findUnique();
	}
	public static String getWork_typeJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getWork_type().toString(), v.getWork_type().toString()); 
		}
		return node.toString();
	}
	public static Booking findByName(String d) {
		return find.where().eq("name", d).findUnique();
	}
	public static String getNameJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getName().toString(), v.getName().toString()); 
		}
		return node.toString();
	}
	public static Booking findByCheck_number(Integer d) {
		return find.where().eq("check_number", d).findUnique();
	}
	public static String getCheck_numberJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getCheck_number().toString(), v.getCheck_number().toString()); 
		}
		return node.toString();
	}
	public static Booking findByComment(String d) {
		return find.where().eq("comment", d).findUnique();
	}
	public static String getCommentJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getComment().toString(), v.getComment().toString()); 
		}
		return node.toString();
	}
	public static Booking findByWrite_date(Date d) {
		return find.where().eq("write_date", d).findUnique();
	}
	public static String getWrite_dateJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Booking v: find.all()) {
			node.put(v.getWrite_date().toString(), v.getWrite_date().toString()); 
		}
		return node.toString();
	}
}
